/*
 * Copyright (c) 2011-2023, ThinkWide (cnet2001@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.thinkwide.data.ormapping.db;

import com.thinkwide.data.core.MFConvert;
import com.thinkwide.data.core.MFPropperties;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 数据库连接配置信息
 *
 * @author AndyZhang 2011
 * @since 2010-12-29
 */
public class MFConnectionConfig {
    private static final String def = "default";
    private static final String driverClasses = "driverClasses";
    private static final String adapterClasses = "adapterClasses";
    private static final String dbType = "dbType";
    private static final String url = "url";
    private static final String user = "user";
    private static final String password = "password";
    private static final String dbVersion = "dbVersion";

    private static final String charset = "charset";
    private static final String initialPoolSize = "initialPoolSize";
    private static final String maxIdleTime = "maxIdleTime";
    private static final String maxPoolSize = "maxPoolSize";
    private static final String minPoolSize = "minPoolSize";
    private static final String acquireIncrement = "acquireIncrement";
    private static final String checkoutTimeout = "checkoutTimeout";


    public static MFConnectionInfo loadConnection(String name) throws Throwable {
        MFPropperties prop = new MFPropperties("data.properties", false);
        prop.loadData();

        String URL = prop.getPropValue(name + "." + url);
        if (URL != null) {
            String AdapterClasses = prop.getPropValue(name + "." + adapterClasses);
            String DbType = prop.getPropValue(name + "." + dbType);
            String DbVersion = prop.getPropValue(name + "." + dbVersion);
            String USER = prop.getPropValue(name + "." + user);
            String PASS = prop.getPropValue(name + "." + password);
            String DC = prop.getPropValue(name + "." + driverClasses);
            String Charset = prop.getPropValue(name + "." + charset);

            Integer Initialpoolsize = MFConvert.strToIntDef(prop.getPropValue(name + "." + initialPoolSize), 8);
            Integer Maxidletime = MFConvert.strToIntDef(prop.getPropValue(name + "." + maxIdleTime), 600);
            Integer Maxpoolsize = MFConvert.strToIntDef(prop.getPropValue(name + "." + maxPoolSize), 6000);
            Integer Minpoolsize = MFConvert.strToIntDef(prop.getPropValue(name + "." + minPoolSize), 8);
            Integer Acquireincrement = MFConvert.strToIntDef(prop.getPropValue(name + "." + acquireIncrement), 10);
            Integer Checkouttimeout = MFConvert.strToIntDef(prop.getPropValue(name + "." + checkoutTimeout), 3600);

            MFConnectionInfo info = new MFConnectionInfo();
            info.setDbType(DbType);
            info.setDbVersion(DbVersion);
            info.setDbName(name);
            info.setAdapterClasses(AdapterClasses);
            info.setDbType(DbType);
            info.setDriverClasses(DC);
            info.setPassword(PASS);
            info.setUrl(URL);
            info.setUser(USER);

            info.setCharset(Charset);

            info.setCheckoutTimeout(Checkouttimeout);
            info.setMaxIdleTime(Maxidletime);
            info.setInitialPoolSize(Initialpoolsize);
            info.setMinPoolSize(Minpoolsize);
            info.setMaxPoolSize(Maxpoolsize);
            info.setAcquireIncrement(Acquireincrement);

            return info;
        } else {
            return null;
        }
    }

    public static Connection getConnection(String name) throws Throwable {
        MFConnectionInfo info = loadConnection(name);
        if (info != null) {
            String URL = info.getUrl();
            String DRIVER = info.getDriverClasses();
            String USERNAME = info.getUser();
            String PASSWORD = info.getPassword();

            Class.forName(DRIVER);
            Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            return conn;
        }
        return null;
    }

    public static Connection getConnectionSpring() throws Throwable {
        //DataSource dataSource = SpringContextUtils.getBean(DataSource.class);
        //dataSource.getConnection()
        return null;
    }
}
